<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="小区名称">
            <el-select
              class="slese"
              @visible-change="xmhandleVisibleChange"
              v-model="queryParams.projectId"
              placeholder="请选择项目"
              ref="selectRef"
              @change="handleQuery"
              clearable
            >
              <el-option class="xmDropdown" v-for="item in xmOptions" :key="item.id" :label="item.projectName" :value="item.id" />
              <p class="el-select-dropdown__empty" v-show="loadingmore">加载中</p>
            </el-select>
          </el-form-item>
          <el-form-item label="车位号" prop="number">
            <el-input v-model="queryParams.number" placeholder="请输入车位号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="车位类型" prop="garageCategory">
            <el-select v-model="queryParams.garageCategory" placeholder="请选择车位类型" style="width: 240px" @change="handleQuery">
              <el-option v-for="dict in garage_category" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="车牌号" prop="garageCarNumber">
            <el-input v-model="queryParams.garageCarNumber" placeholder="请输入车牌号" clearable style="width: 240px" @keyup.enter="handleQuery" />
            </el-form-item> -->
          <el-form-item label="月份" prop="currentDate">
            <el-date-picker v-model="queryParams.currentDate" type="month" placeholder="请选择月份" value-format="YYYY-MM" @change="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="车位物业费周期开始时间" prop="garageManagementStartDate">
            <el-date-picker
              clearable
              v-model="queryParams.garageManagementStartDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择车位物业费周期开始时间"
            />
          </el-form-item>
          <el-form-item label="车位物业费周期停止时间" prop="garageManagementEndDate">
            <el-date-picker
              clearable
              v-model="queryParams.garageManagementEndDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择车位物业费周期停止时间"
            />
          </el-form-item>
          <el-form-item label="车位收车时间" prop="garageCollectionDate">
            <el-date-picker
              clearable
              v-model="queryParams.garageCollectionDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择车位收车时间"
            />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:cost:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="garageFailureList">
        <el-table-column label="小区名称" align="center" prop="projectName" />
        <el-table-column label="房屋号" align="center" prop="houseNumber" />
        <el-table-column label="车位号" align="center" prop="garageNumber" />
        <!-- <el-table-column label="车位面积" align="center" prop="garageArea" /> -->
        <!-- <el-table-column label="车位类型" align="center" prop="garageCategory">
          <template #default="scope">
            <div style="align-items: center">
              <span style="margin-left: 10px" v-if="scope.row.garageCategory==1">出租</span>
              <span style="margin-left: 10px" v-else>自售</span>
            </div>
          </template>
        </el-table-column> -->
        <el-table-column label="车位管理费" align="center" prop="garageManagementCost" />
        <el-table-column label="车牌号" align="center" prop="garageCarNumber" />
        <el-table-column label="是否开具发票" align="center" prop="houseWasteCose">
          <template #default="scope">
            <dict-tag :options="is_need_invoice" :value="scope.row.isNeedInvoice" />
          </template>
        </el-table-column>
        <el-table-column label="车位管理费周期" align="center" prop="garageManagementStartDate" width="300">
          <template #default="scope">
            <span>{{ parseTime(scope.row.garageManagementStartDate, '{y}-{m}-{d}') }}</span>
            <span style="margin:0 10px;">至</span>
            <span>{{ parseTime(scope.row.garageManagementEndDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Cost" lang="ts">
import { failureGarageList } from '@/api/business/failure';
import { CostVO, CostQuery, CostForm } from '@/api/business/parkingLotCost/types';
import { listProject } from '@/api/business/project';
import { ref } from 'vue'
import { formatDate } from '@/utils/index.ts'

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// const { garage_category } = toRefs<any>(proxy?.useDict('garage_category'));
const { is_need_invoice } = toRefs<any>(proxy?.useDict('is_need_invoice'));

const garageFailureList = ref<CostVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const initFormData: CostForm = {
  garageCostId: undefined,
  projectId: undefined,
  garageId: undefined,
  garageNumber: undefined,
  garageArea: undefined,
  garageCategory: undefined,
  garageManagementCost: undefined,
  garageCarNumber: undefined,
  garageManagementStartDate: undefined,
  garageManagementEndDate: undefined,
  // garageCollectionDate: undefined,
  remark: undefined,
  houseId: undefined,
  houseNumber: undefined
}
const data = reactive<PageData<CostForm, CostQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: undefined,
    garageId: undefined,
    garageNumber: undefined,
    number: undefined,
    garageArea: undefined,
    garageCategory: undefined,
    garageManagementCost: undefined,
    garageCarNumber: undefined,
    garageManagementStartDate: undefined,
    garageManagementEndDate: undefined,
    // garageCollectionDate: undefined,
    currentDate: undefined,
    params: {
    }
  },
  rules: {
    garageCostId: [
      { required: true, message: "车位费用id不能为空", trigger: "blur" }
    ],
    projectId: [
      { required: true, message: "小区id不能为空", trigger: "blur" }
    ],
    houseId: [
      { required: true, message: "房屋号不能为空", trigger: "blur" }
    ],
    // garageId: [
    //   { required: true, message: "车位id不能为空", trigger: "blur" }
    // ],
    garageNumber: [
      { required: true, message: "车位号不能为空", trigger: "blur" }
    ],
    // garageArea: [
    //   { required: true, message: "车位面积不能为空", trigger: "blur" }
    // ],
    // garageCategory: [
    //   { required: true, message: "车位类型不能为空", trigger: "blur" }
    // ],
    garageManagementCost: [
      { required: true, message: "车位物业费不能为空", trigger: "blur" }
    ],
    garageCarNumber: [
      { required: true, message: "车牌号不能为空", trigger: "blur" }
    ],
    garageManagementStartDate: [
      { required: true, message: "车位物业费周期开始时间不能为空", trigger: "blur" }
    ],
    garageManagementEndDate: [
      { required: true, message: "车位物业费周期停止时间不能为空", trigger: "blur" }
    ],
    // garageCollectionDate: [
    //   { required: true, message: "车位收车时间不能为空", trigger: "blur" }
    // ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询车位费用列表 */
const getList = async () => {
  loading.value = true;
  const res = await failureGarageList(queryParams.value);
  garageFailureList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.projectId=undefined
  // queryParams.value.garageCategory=undefined
  handleQuery();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/home/excel/getFailureGarageList', {
    ...queryParams.value
  }, `车位管理费逾期_${formatDate(new Date().getTime())}.xlsx`)
}

const xmOptions = ref()
const xmqueryParams = ref<ProjectQuery>({
  pageNum: 1,
  pageSize: 10,
})
const loadingmore = ref(false)
const loadingnomore = ref(false)
const xmhandleVisibleChange = (visible: any) => {
  try {
    if (visible) {
      // 如果下拉框显示
      let xmDropdown: any = document.getElementsByClassName('xmDropdown')[0].parentElement.parentElement
      // 监听下拉框滚动事件
      xmDropdown.addEventListener('scroll', xmloadNextPage)
    } else {
      // 如果下拉框隐藏
      let xmDropdown: any = document.getElementsByClassName('xmDropdown')[0].parentElement.parentElement
      // 移除下拉框滚动事件监听
      xmDropdown.removeEventListener('scroll', xmloadNextPage)
    }
  } catch (error) {}
}

const xmloadNextPage = async () => {
  // let xmDropdown: any = document.querySelector('.el-select-dropdown__wrap');
  let xmDropdown: any = document.getElementsByClassName('xmDropdown')[0].parentElement.parentElement;
  // 判断下拉框是否触底
  if (xmDropdown.scrollTop + xmDropdown.clientHeight >= xmDropdown.scrollHeight - 2) {
    loadingnomore.value = false
    loadingmore.value = true
    if (xmOptions.value.length + 1 <= xmtotal.value && loadingmore.value) {
      xmqueryParams.value.pageNum ++
      const res = await listProject(xmqueryParams.value)
      xmOptions.value.length + 1 <= xmtotal.value?xmOptions.value.push(...res.rows):''
      loadingmore.value = false
    }else{
      loadingmore.value = false
      xmDropdown.removeEventListener('scroll', xmloadNextPage)
      loadingnomore.value = true
    }
  } else {
    loadingnomore.value = true
  }
}

onMounted(async() => {
  getList();
  const xm = await listProject(xmqueryParams.value);
  xmOptions.value = xm.rows
});
</script>
